package com.yx.system.controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.common.validator.Assert;
import com.yx.system.entity.TbWelfare;
import com.yx.system.entity.TbWithdraw;
import com.yx.system.service.TbWelfareService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 作家福利
 *
 * @author luo
 * @email www.baidu.com
 * @date 2018-07-18 10:12:25
 */
@RestController
@RequestMapping("sys/welfare")
public class TbWelfareController {
	@Autowired
	private TbWelfareService welfareService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:welfare:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);

		List<TbWelfare> welfareList = welfareService.queryList(query);
		int total = welfareService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(welfareList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:welfare:info")
	public R info(@PathVariable("id") Integer id){
		TbWelfare welfare = welfareService.queryObject(id);
		return R.ok().put("welfare", welfare);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:welfare:save")
	public R save(String title,String content) throws UnsupportedEncodingException {
		Assert.isBlank(title,"标题不能为空");
		Assert.isBlank(content,"内容不能为空");
		String text=new String(Base64.decodeBase64(content),"utf-8");
		TbWelfare tbWelfare = new TbWelfare();
		tbWelfare.setTitle(title);
		tbWelfare.setContent(text);
		tbWelfare.setCratedate(new Date());
		tbWelfare.setUpdatedate(new Date());
		welfareService.save(tbWelfare);
		return R.ok();
	}


	/*
	 * 保存或修改
	 */
	@RequestMapping("/saveOrUpdate")
	@RequiresPermissions("sys:welfare:saveOrUpdate")
	public R saveOrUpdate(@RequestBody TbWelfare welfare) throws UnsupportedEncodingException{
		Assert.isBlank(welfare.getTitle(),"标题不能为空");
		Assert.isBlank(welfare.getContent(),"内容不能为空");
		if (welfare.getId()==null){
			welfare.setContent(new String(Base64.decodeBase64(welfare.getContent()),"utf-8"));
			welfare.setCratedate(new Date());
			welfare.setUpdatedate(new Date());
			welfareService.save(welfare);
		}else {
			welfare.setContent(new String(Base64.decodeBase64(welfare.getContent()),"utf-8"));
			welfare.setUpdatedate(new Date());
			welfareService.update(welfare);
		}
		return R.ok();
	}


	/**
	 * 修改
	/* *//*
	@RequestMapping("/update")
	@RequiresPermissions("sys:welfare:update")
	public R update(@RequestBody TbWelfare welfare) throws UnsupportedEncodingException {
		welfare.setContent(new String(Base64.decodeBase64(welfare.getContent()),"utf-8"));
		welfare.setUpdatedate(new Date());
		welfareService.update(welfare);
		return R.ok();
	}*/

	@RequestMapping("/update")
	@RequiresPermissions("sys:welfare:update")
	public R update(String title,String content,Integer id) throws UnsupportedEncodingException {
		TbWelfare welfare = welfareService.queryObject(id);
		String text=new String(Base64.decodeBase64(content),"utf-8");
		welfare.setContent(text);
		welfare.setTitle(title);
		welfare.setUpdatedate(new Date());
		welfareService.update(welfare);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:welfare:delete")
	public R delete(@RequestBody Integer[] ids){
		welfareService.deleteBatch(ids);

		return R.ok();
	}

	/*
	* 查询所有福利
	*/
	@RequestMapping("/getAll")
	public R getAll(){
		List<TbWelfare> tbWelfares = welfareService.findAll();
		return R.ok().put("data",tbWelfares);
	}

}